import { Component } from "@angular/core";
import {NzModalService,NzModalSubject,NzNotificationService} from "ng-zorro-antd";
import { Config } from "./../../../service/config";
import { HttpServcie } from "../../../service/http.service";
import {SpotCheckPlan} from "../bean/spotCheckPlan";
import { Router } from "@angular/router";
import * as moment from "moment";



@Component({
  selector: "spot-check",
  templateUrl: "spot-check.html",
  styleUrls: ["spot-check.scss"]
})
export class SpotCheck {

  // 定义数据对象用于界面
  public planData:any=[];
  // 定义计划名称下拉数据类型
  public options:any=[];
  // 定义实施对象下拉数据类型
  public objects:any=[];
  // 定义多选框选择事件
  public allChecked: any = false;
  // 定义多选框
  public indeterminate: any = false;
  // 定义下拉计划值
  public planValue:string="";
  // 定义实施对象下拉值
  public objValue:string="";
  // 定义开始时间
  public startDate:any=null;
  // 定义结束时间
  public endDate:any=null;
  // 定义表头名称
  public modelTitle = "新增";
  // 定义模板类型
  public modal: any = 0;
  // 定义模板服务
  public currentModal: NzModalSubject;
  //表格
  public config: any = Config;
  // 定义初始化实体类
  public spotCheckPlan:SpotCheckPlan=new SpotCheckPlan();
  // 定义总条数
  public total:any;
  // 定义选中的数据
  public clickItemDate:any=[];

  // 定义点击的行数据
  public clumeData:any;


  constructor(

    private httpServcie: HttpServcie,
    private _notification: NzNotificationService,
    private nsub: NzModalSubject,
    private router: Router,
    private modalService: NzModalService
  ) {
    JSON.parse(window.localStorage.getItem("user"));
    this.spotCheckPlan.pageSize = 10;
    this.spotCheckPlan.currentPage = 1;
  }

  ngOnInit() {

    /**
     * 初始化下拉列表
     */
    this.selectOptions();
    /**
     * 初始化数据表格
     */
    this.queryData();


  }
  //查询
  queryData() {

    let spotCheckPlan = new SpotCheckPlan();
    if (this.planValue != "" && this.planValue != undefined && this.planValue != null) {
      spotCheckPlan.planName = this.planValue;
    }
    if (this.startDate != "" && this.startDate != undefined && this.startDate != null) {
      spotCheckPlan.planStartTime =moment(this.startDate).format("YYYYMMDD");
    }

    if (this.endDate != "" && this.endDate != undefined && this.endDate != null) {
      spotCheckPlan.planEndTime =moment(this.endDate).format("YYYYMMDD");
    }
    if ((this.startDate != null && this.startDate != undefined) && (this.endDate != null && this.endDate != undefined)) {
      if (this.startDate > this.endDate) {
        this._notification.create("warning", "警告提示", "结束时间需要大于开始时间！");
        return false;
      }
    }
    spotCheckPlan.partyId = JSON.parse(window.localStorage.getItem("user")).partyId;
    spotCheckPlan.currentPage = this.spotCheckPlan.currentPage;
    spotCheckPlan.pageSize = this.spotCheckPlan.pageSize;
    this.httpServcie.querySpotPlan(spotCheckPlan).subscribe(data => {
      if (data.status == "200") {
        console.log(data);
        if (data.data) {
          this.planData = data.data.list;
          this.total = data.data.total;
        } else {
          this.planData = [];
        }
      } else {
        this._notification.create("error", "错误提示", data.message);
      }
    });
  }
  // 下拉列表
  selectOptions(){
    let list:any[]=[];
    let objs:any[]=[];
    let spotCheckPlan = new SpotCheckPlan();
    this.httpServcie.querySpotPlan(spotCheckPlan).subscribe(data => {
      if (data.status == "200") {
          console.log(data);
          for (let i = 0; i < data.data.list.length; i++) {
            if (list.indexOf(data.data.list[i].planName) == -1) {
              list.push(data.data.list[i].planName);
            }

            /*if(objs.indexOf(data.data.list[i].implementObject) ==-1 ){
              objs.push(data.data.list[i].implementObject)
            }*/
          }
          this.options = list;
         /* this.objects = objs;*/
      } else {
        this._notification.create("error", "错误提示", data.message);
      }
    });
  }
  // 删除功能
  delete(){
    const deleteList: any = [];
    const thiss:any = this;

    for(let i=0;i<this.planData.length;i++){
      if (this.planData[i].checked) {
        deleteList.push(this.planData[i].id + "");
      }
    }
    if (deleteList.length == 0) {
      // 异常
      this._notification.create("warning", "警告提示", "请选中一条数据！");
      return;
    }

    for (let i = 0; i < this.planData.length; i++) {
      if (this.planData[i].checked) {
        if (this.planData[i].isSendMessage == "Y") {
          let name = this.planData[i].planName;
          this._notification.create("waring", "警告提示", name + " 计划已经发送通知了，不能删除，请重新勾选！");
          return;
        }
      }
    }
    this.modalService.confirm({
      title: "您是否确认要删除这 " + deleteList.length + " 条内容",
      content: "<b>一旦删除，无法还原</b>",
      onOk() {
        let userId = JSON.parse(window.localStorage.getItem("user")).id;
        let ob = thiss.httpServcie.deleteSpotPlan({ids: deleteList,userId:userId}); //批量删除
        ob.subscribe(
          data => {
            if (data.status == 200) {
              thiss._notification.create("success", "成功", "删除成功！");
              thiss.queryData();
            } else {
              thiss._notification.create("error", "错误提示", data.message);
            }
          },
          error => {
            thiss._notification.create("error", "错误提示", "删除失败！");
          }
        );
      },
      onCancel() {
      }
    });
  }
  // 添加界面打开
  add(titleTpl, contentTpl, footerTpl) {
    this.modelTitle = "添加";
    this.modal = 0;
    this.currentModal = this.modalService.open({
      title: titleTpl,
      content: contentTpl,
      footer: footerTpl,
      maskClosable: false,
      width: 900,
      style: {
        top: "20px"
      },
      onOk() {
        console.log("Click ok");
      }
    });
  }
  // 添加后界面刷新
  addOutPut(event) {
    setTimeout(() => {
      /* destroy方法可以传入onOk或者onCancel。默认是onCancel */
      this.currentModal.destroy();
      this.currentModal = null;
    }, 200);
    // this.elevatorlBean.currentPage = 1;
    this.queryData();
  }
  // 添加检查项
  importCheckItem(title, content, footer, data){
    const updateList: any = [];
    const checkoutList: any = [];
    this.planData.forEach(element => {
      // if (element.checked) {
      //   updateList.push(element.id);
      // }
      //  去掉没选中状态的提示信息，通过data.id和element.id是否相等来判断
      if (element.id == data.id) {
        updateList.push(element.id);
      }
       if (element.checked) {
        checkoutList.push(element.id);
      }
    });
    // if(updateList.length==0 || updateList == null){
    //   this._notification.create("warning", "错误提示", "请先勾选您要添加的数据！");
    //   return  false;
    // }
    if(checkoutList.length > 1 || checkoutList == null){
      this._notification.create("warning", "警告提示", "不需选中就可以添加检查项！");
      // return  false;
    }
    this.clickItemDate = updateList;
    this.modelTitle = "检查项目";
    this.modal = 1;
    this.currentModal = this.modalService.open({
      title: title,
      content: content,
      footer: footer,
      maskClosable: false,
      width: 800,
      style: {
        top: "40px"
      },
      onOk() {
        console.log("Click ok");
      }
    });
  }

  addObject(title, content, footer, data){
    if(data.isCheck == "N"){
      this._notification.create("warning", "警告提示", "请先添加检查项！");
      return;
    }
    this.clumeData = data;

    this.modelTitle = "添加";
    this.modal = 2;
    this.currentModal = this.modalService.open({
      title: title,
      content: content,
      footer: footer,
      maskClosable: false,
      width: 800,
      style: {
        top: "40px"
      },
      onOk() {
        console.log("Click ok");
      }
    });
  }


  // 查看进行任务下达
  showPD(data){
    if(data.isCheck=="N"){
      this._notification.create("warning", "警告提示", "请先添加检查项！");
      return;
    }
    this.router.navigate(["/home/spot-check-detail", data]);
  }


  //全选
  checkAll(value) {
    if (value) {
      this.planData.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.planData.forEach(data => (data.checked = false));
    }
  }

  //table加载数据时
  displayDataChange($event) {
    this.planData = $event;
    this.refreshStatus();
  }

  //重置选择框
  refreshStatus() {
    const allChecked = this.planData.every(
      value => value.disabled || value.checked
    );
    const allUnChecked = this.planData.every(
      value => value.disabled || !value.checked
    );
    this.allChecked = allChecked;
    this.indeterminate = !allChecked && !allUnChecked;
  }


  //分页
  nzPageSizeChange(event) {
    this.spotCheckPlan.pageSize = event;
    this.spotCheckPlan.currentPage = 1;
    this.queryData();
  }

  //输入框的方法
  nzPageIndexChange(event) {
    this.spotCheckPlan.currentPage = event;
    this.queryData();
  }

  //当前页
  nzPageIndexClickChange(event) {
    this.spotCheckPlan.currentPage = event;
    this.queryData();
  }




}
